package it.polimi.swim.servlet;

import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.HelpRequestManagerBean;
import it.polimi.swim.session.HelpRequestManagerBeanRemote;
import it.polimi.swim.session.SkillManagerBean;
import it.polimi.swim.session.SkillManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBean;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HelpRequestNotificationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public HelpRequestNotificationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("post");
			
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			System.out.println("Chiamo il sesison");
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup(HelpRequestManagerBean.REMOTE);
			HelpRequestManagerBeanRemote helpRequestManager = (HelpRequestManagerBeanRemote) ref;
			Object ref2 = ctx.lookup(SystemUserManagerBean.REMOTE);
			
			SystemUserManagerBeanRemote SystemUserManager = (SystemUserManagerBeanRemote) ref2;
			HttpSession session = request.getSession();
			
			SystemUser user = SystemUserManager.searchUserById((Integer) session.getAttribute("id"));
			Set<HelpRequest> helps = (Set<HelpRequest>) user.getOtherHelp();
			
			request.setAttribute("otherHelpRequest", helps);
			Set<HelpRequest> myHelps = (Set<HelpRequest>) user.getCreator();
			request.setAttribute("myHelpRequest", myHelps);
			request.getRequestDispatcher("/helpRequestNotification.jsp").forward(request, response);

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
